package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Constants;
import com.bjpowernode.crm.commons.domain.ResultEntity;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Clue;
import com.bjpowernode.crm.workbench.domain.ClueActivityRelation;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.service.ClueActivityRelationService;
import com.bjpowernode.crm.workbench.service.ClueService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 郭俊才
 * 2021/8/22
 */
@Controller
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private UserService userService;
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ClueActivityRelationService clueActivityRelationService;

    @ResponseBody
    @RequestMapping("/workbench/clue/getPageInfo.do")
    public ResultEntity getPageInfo(Clue clue, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageInfo<Clue> pageInfo = clueService.queryByConditions(clue, pageNum, pageSize);
        if (pageInfo.getList() != null) {
            return ResultEntity.successWithData(pageInfo);
        } else {
            return ResultEntity.failed("没有数据!");
        }
    }

    //进入市场活动页面请求用户信息
    @RequestMapping("/workbench/clue/index.do")
    public String toClue(Model model, HttpSession session) {
        //获取用户信息
        List<User> users = userService.queryAllUser();
        List<User> userList = userService.queryAllUser();
        User user = (User) session.getAttribute(Constants.MESSAGE_SESSION_NAME);
        //移除掉当前用户的信息，当前用户的信息从session域中取
        for (int i = 0; i < users.size(); i++) {
            if (user.getId().equals(users.get(i).getId())) {
                users.remove(i);
                break;
            }
        }
        //从字典值中获取称呼
        List<DicValue> appellationList = dicValueService.queryByTypeCode("appellation");
        //从字典值中获取线索状态
        List<DicValue> stateList = dicValueService.queryByTypeCode("clueState");

        //从字典值中获取线索来源
        List<DicValue> sourceList = dicValueService.queryByTypeCode("source");
        model.addAttribute("users", users);
        model.addAttribute("userList", userList);
        model.addAttribute("appellationList", appellationList);
        model.addAttribute("stateList", stateList);
        model.addAttribute("sourceList", sourceList);
        return "workbench/clue/index";
    }

    //
    //添加
    @RequestMapping("/workbench/clue/createClue.do")
    @ResponseBody
    public ResultEntity createActivity(Clue clue, HttpSession session) {
        User user = (User) session.getAttribute(Constants.MESSAGE_SESSION_NAME);
        //设置创建人
        clue.setCreateBy(user.getId());
        int saveNum = clueService.saveSelective(clue);
        if (saveNum > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed("添加失败");
        }
    }

    @RequestMapping("/workbench/clue/dropClue.do")
    @ResponseBody
    public ResultEntity dropActivity(@RequestBody String[] ids) {

        int i = clueService.removeList(ids);
        if (i > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed("删除失败");
        }
    }

    //
    @RequestMapping("/workbench/clue/changeClue.do")
    @ResponseBody
    public ResultEntity changeActivity(Clue clue, HttpSession session) {
        //设置修改人
        User user = (User) session.getAttribute(Constants.MESSAGE_SESSION_NAME);
        clue.setEditBy(user.getId());
        int i = clueService.editByPrimaryKeySelective(clue);
        if (i > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed("修改失败");
        }
    }

    @RequestMapping("/workbench/clue/getClue.do")
    @ResponseBody
    public ResultEntity getClue(String id) {
        Clue clue = clueService.queryByPrimaryKey(id);
        if (clue != null) {
            return ResultEntity.successWithData(clue);
        } else {
            return ResultEntity.failed("查询失败");
        }
    }

    @RequestMapping("/workbench/clue/getClueForDetails.do")
    public String getActivityForDetails(String id, Model model) {
        //根据线索id获取线索详情
        Clue clue = clueService.queryByPrimaryKeyForDetails(id);
        //根据线索id获取线索评论列表
        //根据线索id获取市场活动列表
        List<Activity> activityList = activityService.queryActivityByCluePrimaryKey(id);
        model.addAttribute("clue",clue);
        model.addAttribute("activityList",activityList);
        return "workbench/clue/detail";
    }
    //从线索市场活动关系表中删除
    @ResponseBody
    @RequestMapping("/workbench/clue/dropUnbundActivity.do")
    public ResultEntity dropUnbundActivity(String activityId,String clueId) {
        int removeNum = clueActivityRelationService.removeClueActivityRelationById(clueId, activityId);
        if (removeNum > 0) {
            return ResultEntity.successWithoutData();
        } else {
            return ResultEntity.failed("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/searchActivityNoBundById.do")
    public ResultEntity searchActivityNoBundById(String activityName,String clueId) {
        List<Activity> activityList = activityService.queryByNameAndClueId(activityName, clueId);

        if (activityList.size() > 0) {
            return ResultEntity.successWithData(activityList);
        } else {
            return ResultEntity.failed("查询失败");
        }
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/searchActivityBundById.do")
    public ResultEntity queryActivityForDetailByName(String clueId) {
        List<Activity> activityList = activityService.queryActivityByCluePrimaryKey(clueId);
        if (activityList.size() > 0) {
            return ResultEntity.successWithData(activityList);
        } else {
            return ResultEntity.failed("查询失败");
        }
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/saveBundActivity.do")
    public ResultEntity saveBundActivity(String[] activityId,String clueId) {
        //将activityId和clueId拼成一个list,便于执行批量插入操作
        ArrayList<ClueActivityRelation> clueActivityRelationList = new ArrayList<>();
        for (String aid : activityId) {
            ClueActivityRelation clueActivityRelation = new ClueActivityRelation(UUIDUtils.getUUID(), clueId, aid);
            clueActivityRelationList.add(clueActivityRelation);
        }
        int saveByList = clueActivityRelationService.saveByList(clueActivityRelationList);
        //根据市场活动id查询市场活动
        List<Activity> activityList = activityService.queryByIds(activityId);
        if (saveByList > 0) {
            return ResultEntity.successWithData(activityList);
        } else {
            return ResultEntity.failed("关联失败");
        }
    }
    @RequestMapping("/workbench/clue/convertClue.do")
    public String convertClue(String clueId,Model model) {
        //跳到转换线索页面时，将当前线索信息和阶段字典值信息带过去
        Clue clue = clueService.queryByPrimaryKeyForDetails(clueId);
        List<DicValue> stageList = dicValueService.queryByTypeCode("stage");
        model.addAttribute("clue",clue);
        model.addAttribute("stageList",stageList);
        return "workbench/clue/convert";
    }
    @ResponseBody
    @RequestMapping("/workbench/clue/saveConvertClue.do")
    public ResultEntity saveConvertClue(String clueId,String isCreateTran,String amountOfMoney,String tradeName,String expectedClosingDate,String stage,String activityId,HttpSession session) {
        User user = (User) session.getAttribute(Constants.MESSAGE_SESSION_NAME);
        //将信息封装到一个map中
        HashMap<String, Object> map = new HashMap<>();
        map.put("clueId", clueId);
        map.put("isCreateTran", isCreateTran);
        map.put("amountOfMoney", amountOfMoney);
        map.put("tradeName", tradeName);
        map.put("expectedClosingDate", expectedClosingDate);
        map.put("stage", stage);
        map.put("activityId", activityId);
        map.put("user", user);
        int i = clueService.saveConvertClue(map);
        if(i > 0){
            return ResultEntity.successWithoutData();
        }else {
            return ResultEntity.failed("转换失败");
        }

    }


}
